//
//  HELPrepareForExamVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELPrepareForExamVC.h"
#import "HELPrepareForExamCell.h"
#import "HELPrepareForExamHeaderView.h"
#import "HELPrepareForExamModel.h"
#import "NSString+Utilities.h"
#import "UIImageView+WebCache.h"
#import "HELWebViewController.h"
#import <UMMobClick/MobClick.h>
#import "HELSuperTableView.h"
#import "BFPaperButton.h"
#import "HELExamModel.h"
#import "BTBaseWebVC.h"
#import "HELExamWebVC.h"
static NSString *cellID = @"HELPrepareForExamCellID";

@interface HELPrepareForExamVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
@property(nonatomic, strong)HELExamModel *prepareModel;
@property(nonatomic, copy)NSString *examId;
@end

@implementation HELPrepareForExamVC

- (instancetype)initWithExamId:(NSString *)examId {
    if (self = [super init]) {
        self.examId = examId;
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"准备考试"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELPrepareForExamCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
//    [self addTableHeadView];
    [self footerAddBtnWith:@"开始考试"];
    [self requestData];
}


- (void)addTableHeadView {
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"HELPrepareForExamHeaderView" owner:nil options:nil];
    HELPrepareForExamHeaderView *tableHeadView = [nibContents lastObject];
    CGFloat f = kScreenFactor;
    tableHeadView.bounds = CGRectMake(0, 0, kScreenWidth, 160 * f);
    
    self.tableView.tableHeaderView = tableHeadView;
//    HELUserInfoM *userInfo = [HELAppManager userInfo];
//    if (!userInfo.userId) {
//        cwqAlertMessage(@"用户id获取失败");
//        return;
//    }
}



- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kTableView_bg;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}


- (void)footerAddBtnWith:(NSString *)title{
    
    BFPaperButton *footerBtn =[BFPaperButton buttonWithType:UIButtonTypeSystem];
    [footerBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    footerBtn.frame =CGRectMake(0, kScreenHeight - 64 - 49, kScreenWidth, 49);
    footerBtn.isRaised = YES;
    
    [footerBtn setTitle:title forState:UIControlStateNormal];
    [footerBtn setBackgroundColor:kMainColor];
  

    footerBtn.tag = 900;
    [footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerBtn addTarget:self action:@selector(beginToExam) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:footerBtn];
}
- (void)beginToExam {

    [self queryIsAbletoExame];
    
}



- (void)queryIsAbletoExame{
  
    
    cwqHud(self.view);
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helGetExamUrl,self.examId] parameters:nil success:^(id responseObject) {
        [hud hide:YES];
        if ([responseObject[@"success"] integerValue]) {
            NSString * examUrl = responseObject[@"data"];
            HELExamWebVC *vc =[HELExamWebVC new];
            vc.theUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[HELNetworkManager sharedMg].bathURL,examUrl]];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        
    } failure:^(NSString *err) {
         [hud hide:YES];
        
    }];

}


- (void)requestData {
    
    
    NSString *url_part =[NSString stringWithFormat:@"%@/%@",helPrepareForExamUrl,self.examId];
    weakSelf_define;
     [self wsj_showLodingGif];
   
     [[HELNetworkManager sharedMg] GET:url_part parameters:nil success:^(id responseObject) {
         [weakSelf wsj_hideLodingGif];
         
         if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
             weakSelf.prepareModel = [HELExamModel mj_objectWithKeyValues:responseObject[@"data"]];
             weakSelf.title = weakSelf.prepareModel.title;
             [weakSelf.tableView reloadData];
         }else{
            [weakSelf wsj_showLoadFailviewWithRetry:^{
                [weakSelf requestData];
            }];
         }
         
         
     } failure:^(NSString *err) {
        [weakSelf wsj_hideLodingGif];
         
     }];
    
    
    
    

}

#pragma mark - <UITableViewData and UITableDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section== 2) {
        return 178;
    }
    return 145;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELPrepareForExamCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.row2Line1.hidden  =YES;
    cell.row2Line0.hidden = YES;
    HELExamModel *model = self.prepareModel;
    switch (indexPath.section) {
        case 0:
        {
            cell.title.text = @"考试内容";
            [cell.leftIcon setImage:[UIImage imageNamed:@"考试内容_考试"]];
            cell.row0Line0.text = @"考题数量";
            if ([model.totalQuestions integerValue] > 0) {
                cell.row0Line1.text = [NSString stringWithFormat:@"%@题",model.totalQuestions];
            }
            if ([model.lengthOfEaxm integerValue] > 0) {
                
                cell.row1Line1.text = [NSString stringWithFormat:@"%@分钟",@([model.lengthOfEaxm integerValue]/60)];
            }
            cell.row1Line0.text = @"考试时间";
        
        }
            break;
        case 1:
        {
            cell.title.text = @"合格标准";
            [cell.leftIcon setImage:[UIImage imageNamed:@"合格__考试"]];
            cell.row0Line0.text = [NSString stringWithFormat:@"%@分满分", model.fullMarks];
            cell.row0Line1.text = @"";
            cell.row1Line0.text = [NSString stringWithFormat:@"%@分及格", model.passScore];
            
            
        }
            break;

        case 2:
        {
            cell.row2Line1.hidden = NO;
            cell.row2Line0.hidden = NO;
            cell.title.text = @"开放时间";
            [cell.leftIcon setImage:[UIImage imageNamed:@"时间_考试"]];
            cell.row0Line0.text = @"开始时间";
            cell.row0Line1.text = [NSString timeToString:[model.beginOpenTime longLongValue]];
            cell.row1Line0.text = @"结束时间";
            cell.row1Line1.text = [NSString timeToString:[model.endOpenTime longLongValue]];;
            cell.row2Line1.text = [NSString stringWithFormat:@"%@/%@",model.useExamCount,model.totalcount];
        }
            break;

            
            
            
        default:
            break;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[UIView new];
    view.frame =CGRectMake(0, 0, kScreenWidth, 17);
    view.backgroundColor =[UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 16;
}





@end
